package my.ts.code;

import my.ts.code.listener.ConfirmListener;
import my.ts.code.net.AppContext;
import my.ts.code.net.InComingListener;
import my.ts.code.objects.Root;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class Splash extends BaseActivity implements ConfirmListener,
		InComingListener {
	/** Called when the activity is first created. fdfd */

	InComingListener listener;
	public static String accId = null;
	Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		//
		showDialogLoading(this, "Connecting to Server");
		listener = this;
		context = this;
		setCListener(this);

		Thread t = new Thread() {
			public void run() {

				try {
					AppContext.instance.session.setListenerForSocket(listener);
					AppContext.instance.connect();
					AppContext.instance.session.setListener(listener);					

					if (AppContext.instance.connected) {
						Message msg = handler.obtainMessage(1);
						handler.sendMessage(msg);
					} else {
						Message msg = handler.obtainMessage(2);
						handler.sendMessage(msg);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		t.start();

	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			hideDialogLoading();

			if (msg.what == 1) {
//				accId = getAccId();// "100075";//getAccId();
				accId = "100075";
				// System.out.println("ACCId=" + accId);
				if (accId != null) {
					Config.TSM = accId;
					boolean active = getAccActivated();
					if (active) {
						startActivity(new Intent(Splash.this,
								HomepageScreen.class));
						finish();
					} else {
						showDialogConfirmSMS(context, Splash.this,
								"Kích hoạt ứng dụng", "Thông báo",
								Config.ACTIVE_SYNTAX.getSyntax()
										+ Config.AppCode + " " + accId,
								Config.ACTIVE_SYNTAX.getNumber(), false, true);
					}
				} else {
					AppContext.instance.session.getAccId();
				}
			} else if (msg.what == 2) {
				// Toast.makeText(getBaseContext(), "Kết nối không thành công",
				// 3000).show();
				// finish();
				dialog.dismiss();
				showDialodElert(context, Splash.this, "Thông báo",
						"Lỗi kế nối máy chủ, đề nghị kiểm tra kế nối mạng",
						true);

			} else if (msg.what == 3) {
				Root root = (Root) msg.obj;
				saveAccId(root.getItems().get(0).getAccountID());
				accId = getAccId();
				Config.TSM = accId;
				showDialogConfirmSMS(context, Splash.this,
						"Kích hoạt ứng dụng", "Thông báo",
						Config.ACTIVE_SYNTAX.getSyntax() + Config.AppCode + " "
								+ accId, Config.ACTIVE_SYNTAX.getNumber(),
						false, true);
			}
		}
	};

	@Override
	public void dispatch(String data) {
		// TODO Auto-generated method stub
		Root root = parse(data);
		Message msg = handler.obtainMessage(3, root);
		handler.sendMessage(msg);
	}

	@Override
	public void connectionError(boolean error) {
		if (error) {
			Message msg = handler.obtainMessage(2);
			handler.sendMessage(msg);
		}
	}

	public static final String MYPREFS = "mykey";
	public static final String ACTIVATED = "activated";

	protected String getAccId() {
		SharedPreferences saveUi = getPreferences(MODE_PRIVATE);
		return saveUi.getString(MYPREFS, null);
	}

	protected boolean getAccActivated() {
		SharedPreferences saveUi = getPreferences(MODE_PRIVATE);
		return saveUi.getBoolean(ACTIVATED, false);
	}

	protected void saveAccId(String accid) {
		// Create or retrieve the shared preference object.
		System.out.println("ACCID=" + accid);
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getPreferences(mode);
		SharedPreferences.Editor editor = mySharedPreferences.edit();
		editor.putString(MYPREFS, accid);
		editor.commit();
	}

	protected void saveAccActivated(boolean activated) {
		// Create or retrieve the shared preference object.
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences mySharedPreferences = getPreferences(mode);
		SharedPreferences.Editor editor = mySharedPreferences.edit();
		editor.putBoolean(ACTIVATED, activated);
		editor.commit();
	}

	@Override
	public void confirmListener(boolean done) {
		// TODO Auto-generated method stub
		saveAccActivated(true);
		startActivity(new Intent(Splash.this, HomepageScreen.class));
		finish();
	}
}
